<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>03-canvas</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    html,
    body {
      height: 100%;
    }

    canvas {
      width: 1200px;
      height: 600px;
      background-color: #000;
    }
  </style>
</head>

<body>
  <canvas id="charts"></canvas>

  <script>
    function setupCanvas(canvas) {
      var dpr = (scale = window.devicePixelRatio || 1);
      var rect = canvas.getBoundingClientRect();
      canvas.width = rect.width * dpr;
      canvas.height = rect.height * dpr;
      canvas.style.width = rect.width + "px"
      canvas.style.height = rect.height + "px"
      return canvas;
    }
    const canvas = setupCanvas(document.getElementById('charts'))
    const context = canvas.getContext('2d')
    context.moveTo(10, 10)
    context.lineTo(30, 10)

    context.strokeStyle = 'orange'
    context.stroke()

    const ctx = context

    // 绘制矩形
    ctx.strokeRect(10, 20, 20, 20)
    // 绘制填充矩形：fillRect(x, y, width, height)
    ctx.fillStyle = 'purple'
    ctx.fillRect(10, 50, 10, 10)

    // 绘制圆形
    ctx.beginPath()
    ctx.arc(20, 80, 10, 0, Math.PI * 2, true)
    // ctx.fill() // 填充
    ctx.stroke() // 线条
    ctx.closePath()

    ctx.beginPath() // 清除路径
    ctx.arc(20, 110, 10, 0, Math.PI * 2, true)
    // ctx.fill() // 填充
    ctx.stroke() // 线条
    ctx.closePath() // 清除路径
    // 绘制半圆
    ctx.beginPath()
    ctx.arc(20, 140, 10, 0, Math.PI * 2, false)
    ctx.stroke()
    ctx.closePath()

    // 清除矩形区域
    ctx.clearRect(10, 20, 5, 5)

    // 将图片复制到 canvas 上
    const img = new Image()
    img.src = './imgs/girl.png'
    let n = 0
    // ctx.drawImage(img, 256 * 2, 256 * 4, 256, 256, 0, 0, 256, 256)
    img.onload = () => {
      setInterval(() => {
        ctx.clearRect(0, 0, charts.width, charts.height)
        // ctx.drawImage(img, 256 * n, 256 * 4, 256, 256, 0, 0, 256, 256)
        // ctx.drawImage(img, 256 * n, 256 * 3, 256, 256, 0, 0, 256, 256)
        ctx.drawImage(img, 256 * n, 256 * 2, 256, 256, 0, 0, 256, 256)
        // ctx.drawImage(img, 256 * n, 256 * 1, 256, 256, 0, 0, 256, 256)
        n++
        if (n > 7) {
          n = 0
        }
      }, 100);
    }



  </script>
</body>

</html>